package com.example.sunchuanjia_03;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ProductsListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_products);

        ListView listViewCart = findViewById(R.id.listViewCart);
        String[] cartItems = {"华为mate 60\n8888元", "oppo R13\n4888元", "联想小新16pro\n5888元", "小米12\n3888元"};
        // 创建 ArrayAdapter 并与 ListView 绑定
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, cartItems);
        listViewCart.setAdapter(adapter);

        listViewCart.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里处理点击列表项的逻辑
                String selectedItem = (String) parent.getItemAtPosition(position);
                Intent intent = new Intent(ProductsListActivity.this, ProductDetailsActivity.class);
                String[] data = selectedItem.split("\n");
                intent.putExtra("key", data);
                startActivity(intent);
            }
        });
    }
}